//
//  SingleManager.m
//  HeWeiZhiNeng
//
//  Created by 林建 on 17/12/4.
//  Copyright © 2017年 YanCeyMichael.github.io. All rights reserved.
//

#import "SingleManager.h"
#import <CommonCrypto/CommonDigest.h>
#define UserFilePath  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"User.data"]
BOOL isNotClick = YES;
@implementation SingleManager

+(SingleManager *)shareManager
{
    static SingleManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[SingleManager alloc]init];
    });
    return manager;
}

-(instancetype)init{
    self = [super init];
    if (self) {
        self.NetworkStatus = YES;
        _user =  [NSKeyedUnarchiver unarchiveObjectWithFile:UserFilePath];
    }
    return self;
}

#pragma mark ----- 登出 -----
- (void)logout {
    _user = nil;
    if ([[NSFileManager defaultManager] fileExistsAtPath:UserFilePath]) {
        [[NSFileManager defaultManager]  removeItemAtPath:UserFilePath error:nil];
    }
}

#pragma mark ----- 保存医生信息 -----
- (void)saveUser:(UserModel *)user{
    _user = user;
    [NSKeyedArchiver archiveRootObject:user toFile:UserFilePath];
}

#pragma mark ----- 弹出框弹出方法 -----
-(void)creatPushView:(NSInteger)code VC:(UIViewController *)VC msg:(NSString *)msg{
    if (isNotClick == YES) {
        isNotClick = NO;
        NSString *contentStr = [NSString string];
        if (code == 101) {
            contentStr = @"请输入手机号或者邮箱";
        } else if (code == 102) {
            contentStr = @"请输入正确的手机或者邮箱格式";
        } else if (code == 103) {
            contentStr = @"请输入有效的手机号码！";
        }
       
        CGFloat label_W = [self getTextWidth:contentStr withHeight:50 font:[UIFont boldSystemFontOfSize:14]];
        UILabel *pushLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, label_W+40, 37)];
        pushLabel.center = VC.view.center;
        pushLabel.text = contentStr;
        pushLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
        pushLabel.textColor = [UIColor whiteColor];
        pushLabel.numberOfLines = 0;
        pushLabel.textAlignment = NSTextAlignmentCenter;
        pushLabel.font = [UIFont boldSystemFontOfSize:14];
        pushLabel.layer.cornerRadius = 5;
        pushLabel.layer.masksToBounds = YES;
        UIWindow *window = VC.view.window;
        [window addSubview:pushLabel];
        [UIView animateWithDuration:2.0 animations:^{
            pushLabel.alpha = 0;
        } completion:^(BOOL finished) {
            [pushLabel removeFromSuperview];
            isNotClick = YES;
        }];
    }
}

#pragma mark ----- 计算文本宽度 -----
-(CGFloat)getTextWidth:(NSString *)text withHeight:(CGFloat)height font:(UIFont *)font{
    CGSize newSize = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    
    return newSize.width;
}

#pragma mark ----- MD5加密 -----
-(NSString *)md5:(NSString *)input {
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(cStr, (CC_LONG)strlen(cStr), digest);
    
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [result appendFormat:@"%02X", digest[i]];
    }
    
    return result;
}
@end
